package model.os;

import java.util.HashMap;
import java.util.Map;


public class PageTable {

	private Map<Integer, PageTableEntry> entries;

	public PageTable() {
		entries = new HashMap<Integer, PageTableEntry>();
	}

	public void clear() {
		entries.clear();
	}

	public void addEntry(Integer number, PageTableEntry entry) {
		entries.put(number, entry);
	}

	public void setEntries(Map<Integer, PageTableEntry> entries) {
		this.entries = entries;
	}

	public Map<Integer, PageTableEntry> getEntries() {
		return entries;
	}
	
	public PageTableEntry getPageTableEntry(Integer pageNumber) {
		return entries.get(pageNumber);
	}

	public Address getPhysicalAdress(Address virtualAdddres) {
		PageTableEntry entry = entries.get(virtualAdddres.getNumber());
		if (entry.isValid()) {
			return new Address(entry.getPageFrameNumber(),
					virtualAdddres.getOffset());
		} else {
			return null;
		}
	}

	public void updateEntry(int virtualPageNumber, int pageFrameNumber) {
		PageTableEntry pageTableEntry = entries.get(virtualPageNumber);
		pageTableEntry.setPageFrameNumber(pageFrameNumber);
		pageTableEntry.setValid(true);
		pageTableEntry.setReferenced(true);
	}

}
